package main;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import stripsLanguage.Plan;
import stripsLanguage.Task;
import stripsPlanner.ChooseOperator;
import stripsPlanner.DataStorage;

/**
 * Zapisuje wyniki pod zadana sciezka 
 * @author Lukasz
 *
 */

public class ResultsPrinter {

	public ResultsPrinter() {}
	
	public void printAll(Task task, List<DataStorage> plans, long time, File path) 
		{
		String s = "Total elapsed time in execution of method POP.findPlan() is :" + time + " ms"
			+ System.getProperty("line.separator"); 		
		s += printResults(plans);
		
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(path));
			out.write(s);
			out.close();
		 } catch (IOException e)
		 {
			 System.out.println("saveButtonMouseClicked: write to file error");
		 } 
	}
	
	public String printResults(List<DataStorage> plans)
	{
		String s = "";
		for(DataStorage d: plans)
		{
			s += d.getPlan().toString();
			if(d.getSubgoal() != null)
				s += "\tSubgoal: " + d.getSubgoal().toString() + System.getProperty("line.separator");
			if(d.getStoredChoosing() != null)
			{
				s += d.getStoredChoosing().toString() + System.getProperty("line.separator");
				s += "\tId of ds: " + d.getIdOfDataStorage() + System.getProperty("line.separator");
				s += "\tId of previous ds: " + d.getIdOfPreviousDataStorage() + System.getProperty("line.separator");
			}
		}
		return s;
	}
	
	public String showLinearization(Plan plan)
	{
		String s = "Plan koncowy: " + System.getProperty("line.separator");
		
		List<List<Integer>> matrix = new ChooseOperator().buildLinearization(
			plan.getSteps(),
			plan.getOrderings());
		
		for(int i: matrix.get(0))
			s += plan.getSteps().get(i-1).toString() + System.getProperty("line.separator");
			
		s += "Linearyzacje\n";
		for(List<Integer> list: matrix)
		{
			for(int i: list)
				s += i + ", ";
			s += System.getProperty("line.separator");
		}
		
		return s;
	}
	
}
